package com.efounder.form.comp.commodity;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.LinearLayout;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.application.ApplicationContainer;
import com.efounder.form.base.IComponent;
import com.efounder.form.builder.IScriptObject;
import com.efounder.form.comp.IUIComponent;
import com.efounder.form.comp.shoppingcar.DividerItemDecoration;
import com.efounder.form.model.ColumnModel;
import com.efounder.form.model.MDModel;
import com.efounder.form.model.ShoppingCarFormModel;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.frame.arcmenu.ArcMenu;
import com.efounder.util.AppContext;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Javor on 16/9/15.
 */

public class Commodity_old extends RecyclerView implements IComponent,IUIComponent,DMComponent,IScriptObject {

    public Commodity_old() {
        super(AppContext.getInstance());
    }

    private String id;

    private int height;
    private int width;

    private int gap = 0;

    private ViewSize size = new ViewSize(0, 0);

    private int percentWidth;
    private int percentHeight;

    private String dataSetID;
    private String dataSetColID;

    private Map scriptObject = new HashMap();
    private Map scriptContext = new HashMap();

    private String horizontalAlign;
    private String verticalAlign;

    private DataSetComponent dataSetComponent;
    private EFDataSet dataSet;
    private EFRowSet mainRowSet;
    private ColumnModel columnModel;

    private ShoppingCarFormModel shoppingCarFormModel;

    private IUIComponent parentComp;

    private CommodityCategoryAdapter_old adapter;

    private void init() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        //设置布局管理器
        this.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        //设置分隔线
        this.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        //设置增加或删除条目的动画
        this.setItemAnimator(new DefaultItemAnimator());
        //设置Adapter
        adapter = new CommodityCategoryAdapter_old(this, (MDModel)dataSetComponent);
        this.setAdapter(adapter);
//        this.setOnTouchListener(new OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                ViewParent viewParent = getParent();
//                if (viewParent == null) return false;
//                viewParent.requestDisallowInterceptTouchEvent(true);
//                Log.i("","");
//                return false;
//            }
//        });
    }

    @Override
    public void setLayout() {

    }

    public void dataChanged(EFDataSet dataSet) {
//        this.init();
        adapter.reloadData((MDModel)dataSetComponent);
//        this.getAdapter().notifyDataSetChanged();
    }

    @Override
    public DataSetComponent getDataSetComponent() {
        return dataSetComponent;
    }

    @Override
    public void setDataSetComponent(DataSetComponent dsc) {
        if (dataSetComponent != null)
            dataSetComponent.removeDMComponent(this);
        dataSetComponent = dsc;
        if (dataSetComponent != null)
            dataSetComponent.insertDMComponent(this);
    }


    @Override
    public void creationComplete() {
        FormCompUtil.setLayoutSize(this);
//        this.setLayoutSize();
        this.init();

        //获取ShoppingCarFormModel
        ApplicationContainer applicationContainer = (ApplicationContainer)scriptContext.get("ApplicationContainer");
        if (applicationContainer == null) return;
        shoppingCarFormModel = (ShoppingCarFormModel)applicationContainer.getObject("ShoppingCarFormModel");
//        if (shoppingCarFormModel == null) {
//            shoppingCarFormModel = new ShoppingCarFormModel();
//            EFDataSet dataSet = new EFDataSet();
//            shoppingCarFormModel.getFormDataModel().setBillDataSet(dataSet);
//            applicationContainer.putObject("ShoppingCarFormModel", shoppingCarFormModel);
//            shoppingCarFormModel.creationComplete();
//        }
    }

    /*
    * LinearLayout大小
    */
    public void setLayoutSize() {
        //设置控件宽高
        IUIComponent parent = (IUIComponent) this.getParent();
        int parentWidth = parent.getViewSize().getWidth();
        int parentHeight = parent.getViewSize().getHeight();

        int layoutWidth = (int) (parentWidth * this.percentWidth / 100);
        int layoutHeight = (int) (parentHeight * this.percentHeight / 100);

        layoutWidth = layoutWidth == 0 ? this.width : layoutWidth;
        layoutHeight = layoutHeight == 0 ? this.height : layoutHeight;

        this.size.setWidth(layoutWidth);
        this.size.setHeight(layoutHeight);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(layoutWidth, layoutHeight);
        layoutParams.rightMargin = parent.getGap();
        layoutParams.bottomMargin = parent.getGap();

        this.setLayoutParams(layoutParams);
    }

    public String getHorizontalAlign() {
        return horizontalAlign;
    }

    public void setHorizontalAlign(String horizontalAlign) {
        this.horizontalAlign = horizontalAlign;
    }

    public String getVerticalAlign() {
        return verticalAlign;
    }

    public void setVerticalAlign(String verticalAlign) {
        this.verticalAlign = verticalAlign;
    }

    @Override
    public void setDataSetID(String dataSetID) {
        this.dataSetID = dataSetID;
    }


    @Override
    public String getDataSetID() {
        return dataSetID;
    }

    @Override
    public void setDataSet(EFDataSet dataSet) {
        this.dataSet = dataSet;

        this.dataChanged(dataSet);
    }

    @Override
    public EFDataSet getDataSet() {
        return dataSet;
    }

    @Override
    public EFRowSet getMainRowSet() {
        return mainRowSet;
    }

    @Override
    public void dataSetChanged(DataSetEvent event) {
        dataChanged(event.getDataSet());
    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public int getGap() {
        return gap;
    }

    @Override
    public void setGap(int gap) {
        this.gap = gap;
    }

    @Override
    public void setCompWidth(int width) {
        this.width = width;
    }

    @Override
    public int getCompWidth() {
        return this.width;
    }

    @Override
    public void setCompHeight(int height) {
        this.height = height;
    }

    @Override
    public int getCompHeight() {
        return this.height;
    }

    @Override
    public int getPercentWidth() {
        return percentWidth;
    }

    @Override
    public void setPercentWidth(int percentW) {
        percentWidth = percentW;
    }

    @Override
    public int getPercentHeight() {
        return percentHeight;
    }

    @Override
    public void setPercentHeight(int percentH) {
        percentHeight = percentH;
    }

    @Override
    public ViewSize getViewSize() {
        return size;
    }

    @Override
    public void setViewSize(ViewSize size) {
        this.size = size;
    }

    /**
     * 关联列模型
     * @param columnModel
     */
    public void setColumnModel(ColumnModel columnModel){
        this.columnModel = columnModel;
    }

    public ColumnModel getColumnModel() {
        return columnModel;
    }

    @Override
    public Map getScriptObject() {
        return scriptObject;
    }

    @Override
    public void setScriptObject(Map scriptObject) {
        this.scriptObject = scriptObject;
    }

    @Override
    public String getEventScript(String key) {
        return (String) scriptObject.get(key);
    }

    @Override
    public void setEventScript(String key, String script) {
        scriptObject.put(key, script);
    }

    @Override
    public Map getScriptContext() {
        return scriptContext;
    }

    @Override
    public void setScriptContext(Map context) {
        scriptContext = context;
    }

    public ShoppingCarFormModel getShoppingCarFormModel() {
        if (shoppingCarFormModel == null) {
            //获取ShoppingCarFormModel
            ApplicationContainer applicationContainer = (ApplicationContainer)scriptContext.get("ApplicationContainer");
            if (applicationContainer == null) return null;
            shoppingCarFormModel = (ShoppingCarFormModel)applicationContainer.getObject("ShoppingCarFormModel");
        }
        return shoppingCarFormModel;
    }

    public void setShoppingCarFormModel(ShoppingCarFormModel shoppingCarFormModel) {
        this.shoppingCarFormModel = shoppingCarFormModel;
    }

    public ArcMenu getArcMenu(){
        ApplicationContainer applicationContainer = (ApplicationContainer)scriptContext.get("ApplicationContainer");
        if (applicationContainer == null) return null;
        return (ArcMenu) applicationContainer.getObject("arcMenu");
    }

    @Override
    public IUIComponent getParentComp() {
        return parentComp;
    }

    @Override
    public void setParentComp(IUIComponent comp) {
        parentComp = comp;
    }
}